#include<iostream>
#include<mutex>

class Mutex
{
public:
    Mutex(pthread_mutex_t* lock_p = nullptr):_lock_p(lock_p)// 外面传进来锁
    {}
    void lock()
    {
        if(_lock_p)// 防止外面没有传参
        pthread_mutex_lock(_lock_p);
    }
    void unlock()
    {
        if(_lock_p)
        pthread_mutex_unlock(_lock_p);
    }
    ~Mutex()
    {}
private:
    pthread_mutex_t* _lock_p;
};

class LockGuard // 守卫Lock,出了作用域自动帮我释放资源，防止忘记释放
{
public:
    LockGuard(pthread_mutex_t* lock_p):_mtx(lock_p)// 用外面的锁构造对象
    {
        _mtx.lock();// 在构造时进行加锁
    }
    ~LockGuard()
    {
        _mtx.unlock();// 在析构时进行解锁
    }
private:
    Mutex _mtx;//
};